<template>
    <div class="main-con storeGrouping">
        <!-- 搜索框 -->
        <div class="search-con">
            <div class="search-item">
                <div class="search-item-title">分组名称</div>
                <div class="search-item-input">
                    <el-input size="mini" v-model="formData.name" placeholder="请输入分组名称" @change="handleSearch"></el-input>
                </div>
            </div>
            <div class="search-item">
                <div class="search-item-title"></div>
                <el-button size="mini" type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <el-button size="mini" type="primary" icon="el-icon-delete" @click="handleClear">清空</el-button>
            </div>
        </div>

        <!-- 按钮组 -->
        <div class="btn-group-con">
            <el-button size="mini" type="primary" @click="handleAdd">新增门店分组</el-button>
        </div>

        <!-- 表格数据 -->
        <div class="table-con" v-loading="loading">
            <el-table :data="tableData" size="mini" :header-cell-style="{background:'#f8f8f8'}" style="width: 100%">
                <el-table-column type="index" label="序号" width="50" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="name" label="分组名称" width="" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="count" label="门店数" width="" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="" align="center" :show-overflow-tooltip="true">
                </el-table-column>
                <el-table-column label="操作" width="" align="center">
                    <template slot-scope="scope">
                        <div class="handleBtn">
                            <span @click="handleUpdate(scope.$index, scope.row)">编辑</span>
                            <span @click="handleDelete(scope.$index, scope.row)">删除</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 表格分页 -->
        <div class="pagination-con">
            <m-pagination :currentPage="formData.pageNum" :pageSize="formData.pageSize" :total="total"
                @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange">
            </m-pagination>
        </div>
    </div>
</template>

<script>
import {
    queryStoreGrouping,
    deleteStoreGroupingInfo
} from "@/common/service/storeManagement/storeGrouping.js";
import mPagination from "@/components/m-pagination/m-pagination";
export default {
    name: "storeGrouping",
    data() {
        return {
            // 搜索条件
            formData: {
                name: "", //分组名称
                pageNum: 1,
                pageSize: 10
            },

            //表格数据
            tableData: [], //表格信息
            total: null, //表格总条数
            loading: false //表格加载动画
        };
    },
    created() {
        //初始化表格数据
        this._queryStoreGroupingInfo();
    },
    methods: {
        /*----------------------------查询操作----------------------------*/
        //查询门店分组表格数据
        _queryStoreGroupingInfo() {
            this.loading = true;
            queryStoreGrouping(this.formData)
                .then(res => {
                    this.loading = false;
                    this.tableData = res.content;
                    this.total = res.totalElements;
                })
                .catch(err => {
                    this.loading = false;
                    this.$message.error(err.message);
                });
        },
        //页码切换
        handleCurrentChange(val) {
            this.formData.pageNum = val;
            this._queryStoreGroupingInfo();
        },
        //每页展示条数的切换
        handleSizeChange(val) {
            this.formData.pageSize = val;
            this._queryStoreGroupingInfo();
        },
        //点击查询
        handleSearch() {
            this.formData.pageNum = 1;
            this.formData.pageSize = 10;
            this._queryStoreGroupingInfo();
        },
        //点击清空
        handleClear() {
            this.formData = {
                name: "",
                pageNum: 1,
                pageSize: 10
            };
            this.$message({
                type: "success",
                message: "清空成功!"
            });
            this._queryStoreGroupingInfo();
        },

        /*----------------------------添加操作----------------------------*/
        handleAdd() {
            this.$router.push({
                path: "/index/storeGrouping/editStoreGrouping",
                query: {
                    editType: "add",
                    editText: "新增"
                }
            });
        },

        /*----------------------------编辑操作----------------------------*/
        handleUpdate(index, row) {
            this.$router.push({
                path: "/index/storeGrouping/editStoreGrouping",
                query: {
                    editType: "update",
                    editText: "编辑"
                }
            });
            localStorage.setItem("editData", JSON.stringify(row));
        },

        /*----------------------------删除操作----------------------------*/
        //点击删除
        handleDelete(index, row) {
            this.$confirm("您确定删除门店分组吗？", "删除分组", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    this._deleteStoreGroupingInfo(row);
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        },
        //执行删除操作
        _deleteStoreGroupingInfo(row) {
            var data = {
                id: row.id
            };
            deleteStoreGroupingInfo(data)
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "删除成功!"
                    });
                    this._queryStoreGroupingInfo();
                })
                .catch(err => {
                    this.$message.error(err.message);
                });
        }
    },
    components: {
        "m-pagination": mPagination
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>